package cn.shop.dys.activity;

import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.adapter.NewProductApdapter2;
import cn.shop.dys.bean.Product;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.lib_common.CommonUtil;

public class NewProductActivty2 extends AppCompatActivity {

    private RecyclerView mrcv;
    private NewProductApdapter2 mRcvNewProduAdapter;
    private List<Product> mProducts = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_product_activty2);
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText("新品上架");
        Log.d("新品上架", "onCreate() ----");
        mrcv = (RecyclerView) findViewById(R.id.rcv_newprodu);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        mrcv.setLayoutManager(gridLayoutManager);
        mrcv.addItemDecoration(new SpaceItemDecoration(4));
        HttpUtils httpUtils = new HttpUtils();
        // http://localhost:8080/market/images/product/detail/q8.jpg
        RequestParams requestParams = new RequestParams();
        requestParams.addQueryStringParameter("page", "0");
        requestParams.addQueryStringParameter("pageNum", "10");
        requestParams.addQueryStringParameter("orderby", "saleDown");
        httpUtils.send(HttpRequest.HttpMethod.GET, CommonConstant.NEWPRODUCT_URL, requestParams, mCallBack);
    }

    private RequestCallBack<String> mCallBack = new RequestCallBack<String>() {
        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            String result = responseInfo.result;
            try {
                JSONObject jsonObject = new JSONObject(result);
                String prodJson = jsonObject.getString("productList");
                Gson gson = new Gson();
                Product[] prodArray = gson.fromJson(prodJson, Product[].class);
                for (Product product : prodArray) {
                    mProducts.add(product);
                }
                setAdapter();
                Log.d("新品上架界面", "新品上架json: " + mProducts.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(HttpException e, String s) {

        }
    };

    private void setAdapter() {
        if (mRcvNewProduAdapter == null) {
            mRcvNewProduAdapter = new NewProductApdapter2(this, mProducts);
            mrcv.setAdapter(mRcvNewProduAdapter);
        } else {
            mRcvNewProduAdapter.notifyDataSetChanged();
        }
    }

    public void backPre(View view) {
        finish();
    }

    public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
        int mSpace;
        /**
         * @param space 传入的值，其单位视为dp
         */
        public SpaceItemDecoration(float space) {
            this.mSpace = (int)(CommonUtil.dpToPx(NewProductActivty2.this, space));
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int itemCount = new NewProductApdapter2().getItemCount();
            int pos = parent.getChildAdapterPosition(view);
            Log.d("推荐品牌适配器设置间距类", "itemCount>>" + itemCount + ";Position>>" + pos);

            outRect.left = 2;
            outRect.top = 4;
            outRect.bottom =4 ;


            if (pos != (itemCount - 1)) {
                outRect.right = mSpace;
            } else {
                outRect.right = 2;
            }
        }
    }
}
